//
//  LZPaymentAccountViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/7.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZPaymentAccountViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "LZCouponManagerListDetailSectionView.h"
#import "LZPaymentAccountTextFieldCell.h"
#import "LZRestaurantAuthModel.h"
#import "Masonry.h"

@interface LZPaymentAccountViewController ()<UITableViewDelegate,UITableViewDataSource,LZPaymentAccountCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UIButton *confirmBtn;
@property (assign, nonatomic) BOOL bankSectionIsOpen;
@property (assign, nonatomic) BOOL payPalSectionIsOpen;
@property (strong, nonatomic) LZCouponManagerListDetailSectionView *bankSectionView;
@property (strong, nonatomic) LZCouponManagerListDetailSectionView *payPalSectionView;

@end

@implementation LZPaymentAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureUI];
    [self configureNavBar];
}

#pragma mark -
#pragma mark UI NavBar
- (void)configureUI
{
    self.umLogPageViewName = @"商户认证===账号信息";
    self.title = LZLocalizedString(@"res_auth_pay_account_title", nil);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LZPaymentAccountTextFieldCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    if ([self.authModel.paymentMethod isEqualToString:@"2"]) {
        _bankSectionIsOpen = YES;
    }else if ([self.authModel.paymentMethod isEqualToString:@"1"]){
        _payPalSectionIsOpen = YES;
    }
}

- (void)configureNavBar
{
    self.confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.confirmBtn.frame = CGRectMake(0, 0, 65, 40);
    self.confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.confirmBtn.userInteractionEnabled = NO;
    [self.confirmBtn setTitle:LZLocalizedString(@"confirm", nil) forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:UIColorFromRGB(0x666666, 1) forState:UIControlStateNormal];
    [self.confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    [self navigationItemSetting:@[self.confirmBtn] type:NAVIGATIONITEMSETTING_RIGHT];
    
}


#pragma mark -
#pragma mark private event
- (void)confirmAction
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    if (self.confirmAccountBlock) {
        self.confirmAccountBlock(self.authModel);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)changeNavBtnState
{
    self.confirmBtn.userInteractionEnabled = NO;
    [self.confirmBtn setTitleColor:UIColorFromRGB(0x666666, 1) forState:UIControlStateNormal];

    if ([self.authModel.paymentMethod isEqualToString:@"1"]) {
        if (self.authModel.paypalNumber.length < 2) {
            return;
        }
    }else if ([self.authModel.paymentMethod isEqualToString:@"2"]){
        if (self.authModel.bankNumber.length < 2 || self.authModel.bankName.length < 2) {
            return;
        }
    }
    
    self.confirmBtn.userInteractionEnabled = YES;
    [self.confirmBtn setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];

}
/**
 点击展开银行信息
 */
- (void)clickBankSectionBtn
{
    if (!_bankSectionIsOpen) {
        _bankSectionIsOpen = YES;
        self.authModel.paymentMethod = @"2";
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        
        if (_payPalSectionIsOpen == YES) {
            _payPalSectionIsOpen = NO;
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
}


/**
 点击展开paypal账号信息
 */
- (void)clickPayPalSectionBtn
{
    if (!_payPalSectionIsOpen) {
        _payPalSectionIsOpen = YES;
        self.authModel.paymentMethod = @"1";
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
        
        if (_bankSectionIsOpen == YES) {
            _bankSectionIsOpen = NO;
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
}

#pragma mark -
#pragma mark cell delegate
- (void)getContentText:(NSString *)text withIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            self.authModel.bankName = text;
        }else if (indexPath.row == 1){
            self.authModel.bankNumber = text;
        }
    }else if(indexPath.section == 1){
        self.authModel.paypalNumber = text;
    }
    
    [self changeNavBtnState];
}

#pragma mark -
#pragma mark tableview delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _bankSectionIsOpen ? 2 : 0;
    }
    return _payPalSectionIsOpen ? 1 : 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LZPaymentAccountTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.paymentAccountCellDelegate = self;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [cell loadContent:self.authModel.bankName placeholder:LZLocalizedString(@"res_auth_enter_bank_name", nil) indexPath:indexPath];
        }else{
            [cell loadContent:self.authModel.bankNumber placeholder:LZLocalizedString(@"res_auth_enter_bank_cade_num", nil) indexPath:indexPath];
        }
    }else{
       [cell loadContent:self.authModel.paypalNumber placeholder:LZLocalizedString(@"res_auth_enter_paypal_account", nil) indexPath:indexPath];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        bgView.backgroundColor = [UIColor whiteColor];
        
        self.bankSectionView.nameLabel.text = LZLocalizedString(@"res_auth_bank_account", nil);
        [bgView addSubview:self.bankSectionView];
        
        [self.bankSectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(bgView);
        }];
        
        [self.bankSectionView setRightArrowImgStateIsSelect:_bankSectionIsOpen];
        [self.bankSectionView setLeftImgStateIsSelect:_bankSectionIsOpen];
            
        return bgView;
        
    }else if (section == 1){
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        bgView.backgroundColor = [UIColor whiteColor];
        self.payPalSectionView.nameLabel.text = LZLocalizedString(@"res_auth_paypal_account", nil);
        [bgView addSubview:self.payPalSectionView];
        
        [self.payPalSectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(bgView);
        }];
        
        [self.payPalSectionView setLeftImgStateIsSelect:_payPalSectionIsOpen];
        [self.payPalSectionView setRightArrowImgStateIsSelect:_payPalSectionIsOpen];
        
        return bgView;
    }
    
    return nil;

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        UITableViewHeaderFooterView *v = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        v.contentView.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
        return v;
    }
    
    return nil;
}


#pragma mark -
#pragma mark 初始化
- (LZCouponManagerListDetailSectionView *)bankSectionView
{
    if (!_bankSectionView) {
        _bankSectionView = [[NSBundle mainBundle] loadNibNamed:@"LZCouponManagerListDetailSectionView" owner:self options:nil].lastObject;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBankSectionBtn)];
        [[_bankSectionView getClickAreaView] addGestureRecognizer:tap];
    }
    
    return _bankSectionView;
}

- (LZCouponManagerListDetailSectionView *)payPalSectionView
{
    if (!_payPalSectionView) {
        _payPalSectionView = [[NSBundle mainBundle] loadNibNamed:@"LZCouponManagerListDetailSectionView" owner:self options:nil].lastObject;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPayPalSectionBtn)];
        [[_payPalSectionView getClickAreaView] addGestureRecognizer:tap];
    }
    
    return  _payPalSectionView;
}

@end
